<?php
namespace App\Http\Controllers\Api\v1\Recode;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\v1\User\UserinfoController;
use Illuminate\Support\Facades\Input;
use App\Models\Api\Recode;
use App\Tools\Base\Avatar;

use DB;
// 星盘档案管理控制器
class StarController extends BaseController
{
    public function __construct()
    {

            //parent::__construct();// 接口调用验证

    }
    /**
     * 名人星案提交
     */
    public function add()
    {
        $rid = intval(Input::get('rid'));
        $title = htmlspecialchars(Input::get('title'));
        $description = htmlspecialchars(Input::get('description'));
        $from = htmlspecialchars(Input::get('from'));
        $avatar = htmlspecialchars(Input::get('avatar'));
        if(!$rid || !$title || !$description || !$from || !$avatar)
        {
            return response()->json(['code'=>10601,'msg'=>'Param Error.']);
        }
        $uid = $this->user->id;
        $r_data = Recode::where(['uid'=>$uid,'id'=>$rid])->find();
        if(empty($r_data))
        {
            return response()->json(['code'=>10602,'msg'=>'Recode is not exist.']);
        }

        $time = time();
        $data = [
            'uid'=>$uid,
            'rid'=>$rid,
            'title'=>$title,
            'description'=>$description,
            'from'=>$from,
            'avatar'=>$avatar,
            'create_time'=>$time,
            'last_time'=>$time,
        ];

        $data['id'] = DB::table('app_recode_famous')->insertGetId($data);

        return response()->json(['code'=>200,'data'=>$data,'msg'=>'Success.']);
//        DB::startTrans();
//        try
//        {
//            $time = time();
//            $data = [
//                'uid'=>$uid,
//                'rid'=>$rid,
//                'title'=>$title,
//                'description'=>$description,
//                'from'=>$from,
//                'avatar'=>$avatar,
//                'create_time'=>$time,
//                'last_time'=>$time,
//            ];
//
//            $data['id'] = DB::table('recode_famous')->insertGetId($data);
//
//            DB::commit();
//
//            return response()->json(['code'=>200,'data'=>$data,'msg'=>'Success.']);
//        }
//        catch (\Exception $e)
//        {
//            DB::rollback();
//            return response()->json(['code'=>10603,'msg'=>'Failed.']);
//        }
    }
    /**
     * 编辑名人星案
     */
    public function edit()
    {
        $id = intval(Input::get('id'));
        $rid = intval(Input::get('rid'));
        $title = htmlspecialchars(Input::get('title'));
        $description = htmlspecialchars(Input::get('description'));
        $from = htmlspecialchars(Input::get('from'));
        $avatar = htmlspecialchars(Input::get('avatar'));
        if(!$id || !$rid || !$title || !$description || !$from || !$avatar)
        {
            return response()->json(['code'=>10604,'msg'=>'Param Error.']);
        }
         $uid = $this->user->id;
        $r_data = Recode::where(['uid'=>$uid,'id'=>$rid])->find();
        if(empty($r_data))
        {
            return response()->json(['code'=>10605,'msg'=>'Recode is not exist.']);
        }
        $f_data = DB::table('app_recode_famous')->where(['uid'=>$uid,'rid'=>$rid,'id'=>$id])->find();
        if(empty($f_data))
        {
            return response()->json(['code'=>10606,'msg'=>'Recode famous is not exist.']);
        }
        $time = time();
        DB::table('app_recode_famous')->where(['uid'=>$uid,'rid'=>$rid,'id'=>$id])->update([
            'uid'=>$uid,
            'rid'=>$rid,
            'title'=>$title,
            'description'=>$description,
            'from'=>$from,
            'avatar'=>$avatar,
            'last_time'=>$time,
        ]);

        return response()->json(['code'=>200,'msg'=>'Success.']);

//        DB::startTrans();
//        try
//        {
//            $time = time();
//            DB::table('recode_famous')->where(['uid'=>$uid,'rid'=>$rid,'id'=>$id])->update([
//                'uid'=>$uid,
//                'rid'=>$rid,
//                'title'=>$title,
//                'description'=>$description,
//                'from'=>$from,
//                'avatar'=>$avatar,
//                'last_time'=>$time,
//            ]);
//
//            DB::commit();
//
//            return response()->json(['code'=>200,'msg'=>'Success.']);
//        }
//        catch (\Exception $e)
//        {
//            DB::rollback();
//            return response()->json(['code'=>10607,'msg'=>'Failed.']);
//        }
    }
    /**
     * 查询我的名人星案
     * @return mixed
     */
    public function my()
    {
        $id = intval(Input::get('id'));
        $status = intval(Input::get('status'));
         $uid = $this->user->id;
        $map['uid'] = $uid;
        // 附加查询条件
        if($status)
        {
            $map['status'] = $status;
        }
        if($id)
        {
            $map['id'] = $id;
        }

        $data = DB::table('app_recode_famous') ->where($map)->select();

        return response()->json(['code'=>200,'data'=>$data,'msg'=>'Success.']);
    }
    /**
     * 审核名人星案
     * 审核通过 || 审核未通过
     * @return mixed
     */
    public function changeFamousStatus()
    {
        $id = intval(Input::get('id'));
        $status = intval(Input::get('status'));
        $reason = htmlspecialchars(Input::get('reason'));
        if(!$id || !$reason)
        {
            return response()->json(['code'=>10609,'msg'=>'Param Error.']);
        }
        $uid = $this->user->id;
        $rep = DB::table('app_recode_famous')->where(['uid'=>$uid,'id'=>$id])->find();
        if(empty($rep))
        {
            return response()->json(['code'=>10610,'msg'=>'Recode famous is not exist.']);
        }
        $r_data =Recode::where(['id'=>$rep['rid']])->find();
        if(empty($r_data))
        {
            return response()->json(['code'=>10611,'msg'=>'Recode is not exist.']);
        }

        if($rep['status'] != $status)
        {
            DB::table('app_recode_famous')->where(['uid'=>$uid,'id'=>$id])->update(['status'=>$status]);
            DB::table('app_recode_famous')->where(['uid'=>$uid,'id'=>$id])->setInc('check_times');
        }
        return response()->json(['code'=>200,'msg'=>'Success.']);
//        // 启动事务
//        DB::startTrans();
//        try{
//            if($rep['status'] != $status)
//            {
//                DB::table('recode_famous')->where(['uid'=>$uid,'id'=>$id])->update(['status'=>$status]);
//                DB::table('recode_famous')->where(['uid'=>$uid,'id'=>$id])->setInc('check_times');
//            }
//            // 提交事务
//            DB::commit();
//            return response()->json(['code'=>200,'msg'=>'Success.']);
//        } catch (\Exception $e) {
//            // 回滚事务
//            DB::rollback();
//            return response()->json(['code'=>10612,'msg'=>'Success.']);
//        }
    }
    /**
     * 名人星案列表
     * 查询已通过审核的
     */
    public function lists()
    {
//        $page = intval(Input::get('page'));// 页码
        $pt = intval(Input::get('pt')) ? intval(Input::get('pt')) : 20;// 每页显示数量
        $data = DB::table('app_recode_famous')->from('app_recode_famous as rf')
            ->join('app_recode_remark as rr','rf.id','=','rr.prid','LEFT')
            ->group('rf.id')
            ->where('rf.status=1')
            ->orderBy('rf.last_time','desc')->get(['rf.id','rf.rid','rf.title','rf.description','rf.from','rf.avatar','rf.last_time','rf.create_time'])->paginate($pt,false,['query'=>request()->param()]);

        return response()->json(['code'=>200,'data'=>$data,'msg'=>'ok']);

    }
    /**
     * 评论名人星案
     */
    public function remark()
    {
        $content = htmlspecialchars(Input::get('content'));
        $pid = intval(Input::get('pid'));
        $prid = intval(Input::get('prid'));
        $time = time();
        $uid = $this->user->id;
        $data = [
            'uid'=>$uid,
            'content'=>$content,
            'pid'=>$pid,
            'prid'=>$prid,
            'create_time'=>$time,
        ];
        if(!$uid)
        {
            return response()->json(['code'=>10613,'User is not exist.']);
        }
        if(!$prid)
        {
            return response()->json(['code'=>10614,'Famous is not exist.']);
        }
        if(trim($content) == '')
        {
            return response()->json(['code'=>10615,'Content can not be empty.']);
        }
        $rp_data = DB::table('app_recode_famous')->where(['id'=>$prid,'status'=>1])->find();
        if(empty($rp_data))
        {
            return response()->json(['code'=>10616,'msg'=>'Famous is not exist.']);
        }
        if($pid != 0)
        {
            $p_data = DB::table('app_recode_remark')->where(['id'=>$pid])->find();
            if(empty($p_data))
            {
                return response()->json(['code'=>10617,'msg'=>'Famous remark is not exist.']);
            }
        }
        $data['id'] = DB::table('app_recode_remark')->insertGetId($data);

        return response()->json(['code'=>200,'data'=>$data,'msg'=>'Success.']);
//        DB::startTrans();
//        try{
//            $data['id'] = DB::table('recode_remark')->insertGetId($data);
//            // 提交事务
//            DB::commit();
//            return response()->json(['code'=>200,'data'=>$data,'msg'=>'Success.']);
//        } catch (\Exception $e) {
//            // 回滚事务
//            DB::rollback();
//            return response()->json(['code'=>10618,'msg'=>'Failed.']);
//        }
    }
    /**
     * 添加名人星案背景
     */
    public function avatar($dir = 'uploads')
    {
        $Avatar = new Avatar();

        return $Avatar->upload($dir);
    }
}
